﻿using CompanyHub.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Phone.Management.Deployment;
using CompanyHub.Utilities;
using System.Net;
using Newtonsoft.Json;

namespace CompanyHub.ViewModels
{
    public class CompanyPackageViewModel : INotifyPropertyChanged
    {
        private IEnumerable<CompanyPackage> _packages;

        public CompanyPackageViewModel()
        {
            LoadData();
        }

        public void LoadData()
        {
            // Get list of packages and populate properties
            // _packages = CompanyPackage.GenerateData();

            WebClient client = new WebClient();
            client.Credentials = new NetworkCredential() { UserName = UserName, Password = Password };

            client.DownloadStringCompleted += client_DownloadStringCompleted;
            client.DownloadStringAsync(new Uri("http://169.254.80.80:80/api/packages"));


        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {

            _packages = JsonConvert.DeserializeObject<CompanyPackage[]>(e.Result).ToList();

            UpdatePackageStatus();

        }

        public void UpdatePackageStatus()
        {
            var devicePkgs = InstallationManager.FindPackagesForCurrentPublisher();

            foreach (var pkg in _packages)
            {
                var qry = devicePkgs.Where(p => p.Id.ProductId.Contains(pkg.PackageId.ToString().ToUpper()));
                if (qry.Count() > 0)
                {
                    var devicePkg = qry.First();
                    var devicePkgVersion = PackageVersionHelper.VersionToString(devicePkg.Id.Version);

                    pkg.Status = PackageVersionHelper.IsNewer(pkg.Version, devicePkgVersion) ?
                        CompanyPackageStatus.Update : CompanyPackageStatus.Installed;
                }
                else
                {
                    pkg.Status = CompanyPackageStatus.New;
                }

            }

            _newPackages = _packages.Where(p => p.Status == CompanyPackageStatus.New).ToList();
            _updatePackages = _packages.Where(p => p.Status == CompanyPackageStatus.Update).ToList();
            _installedPackages = _packages.Where(p => p.Status == CompanyPackageStatus.Installed).ToList();

            // Fire notifications for all properties
            NotifyPropertyChanged("NewPackages");
            NotifyPropertyChanged("UpdatePackages");
            NotifyPropertyChanged("InstalledPackages");

        }

        private IEnumerable<CompanyPackage> _newPackages;
        public IEnumerable<CompanyPackage> NewPackages
        {
            get
            {
                return _newPackages;
            }
        }

        private IEnumerable<CompanyPackage> _updatePackages;
        public IEnumerable<CompanyPackage> UpdatePackages
        {
            get
            {
                return _updatePackages;
            }
        }

        private IEnumerable<CompanyPackage> _installedPackages;
        public IEnumerable<CompanyPackage> InstalledPackages
        {
            get
            {
                return _installedPackages;
            }
        }

        public CompanyPackage FindPackage(string id)
        {
            return _packages.Where(p => p.PackageId.ToString() == id).FirstOrDefault();
        }

        private CompanyPackage _selectedPackage;
        public CompanyPackage SelectedPackage
        {
            set
            {
                _selectedPackage = value;
                NotifyPropertyChanged("SelectedPackage");
            }
            get
            {
                return _selectedPackage;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #region Server Creds
        private string UserName = "USERNAME";
        private string Password = "PASSWORD";
        #endregion

    }
}
